from rest_framework import viewsets
import re

from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response

from user import models
from user import serializers


class Login(viewsets.ModelViewSet):
    queryset = models.User.objects.all()
    serializer_class = serializers.UserSerializer

    # 判断手机号是否合格
    @action(detail=False,methods=['POST'])
    def vaphone(self,request):
        phone = request.data.get('phone')
        pwd = request.data.get('pwd')
        print(phone, pwd)
        ret = re.match(r'^1[34578]\d{9}$', phone)
        if ret:
            mod = models.User.objects.filter(phone=phone, password=pwd)
            if mod:
                return Response({'status': 200, 'message': '登录成功'})
            else:
                return Response({'status': 400, 'message': '密码错误'})
            # return Response({'status': 200, 'message': '手机号格式正确'})
        else:
            return Response({'status': 400, 'message': '手机号格式错误'})